package de.playmee.handler;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
	
	private final String TAG = "DatabaseHelper";
	
	private final static String DATABASE_NAME = "playmee";
	private final static int DATABASE_VERSION = 1;
	
    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    	Log.i(TAG, "Creating all tables...");
    	db.execSQL(ChannelHandler.DATABASE_CREATE);
    	db.execSQL(MusicHandler.DATABASE_CREATE_MUSIC);
    	db.execSQL(MusicHandler.DATABASE_CREATE_CHECK);
    	db.execSQL(MusicHandler.DATABASE_CREATE_UPLOAD);
    	db.execSQL(PlaylistHandler.DATABASE_CREATE);
    	db.execSQL(RegionHandler.DATABASE_CREATE);
    	db.execSQL(ChannelTrackRatingHandler.DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data.");
        
        // drop tables
        db.execSQL("DROP TABLE IF EXISTS " + ChannelHandler.DATABASE_TABLE);
        db.execSQL("DROP TABLE IF EXISTS " + MusicHandler.DATABASE_TABLE_MUSIC);
        db.execSQL("DROP TABLE IF EXISTS " + MusicHandler.DATABASE_TABLE_CHECK);
        db.execSQL("DROP TABLE IF EXISTS " + MusicHandler.DATABASE_TABLE_UPLOAD);
        db.execSQL("DROP TABLE IF EXISTS " + PlaylistHandler.DATABASE_TABLE);
        db.execSQL("DROP TABLE IF EXISTS " + RegionHandler.DATABASE_TABLE);
        db.execSQL("DROP TABLE IF EXISTS " + ChannelTrackRatingHandler.DATABASE_TABLE);
        
        // create tables
        onCreate(db);
    }
}    
